<!DOCTYPE html>
<html>
<head>
    <title>编辑 {{ table_name }} 记录</title>
    <link href="/Style/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="/Style/css/bootstrap-icons.css">
    <style>
        body {
            padding: 2rem;
            background-color: #f8f9fa;
        }

    </style>
</head>
<body>
<div class="container">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1><i class="bi bi-pencil-square me-2"></i>编辑 {{ table_name }} 记录</h1>
        <div>
            <!-- 修复：添加蓝图前缀 sqlmanager_bp. -->
            <a href="{{ url_for('sqlmanager_bp.table_view', table_name=table_name) }}"
               class="btn btn-outline-secondary">
                <i class="bi bi-arrow-left me-1"></i> 返回列表
            </a>
        </div>
    </div>

    {% with messages = get_flashed_messages(with_categories=true) %}
    {% if messages %}
    {% for category, message in messages %}
    <div class="alert alert-{{ category }} alert-dismissible fade show" role="alert">
        {{ message }}
        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
    </div>
    {% endfor %}
    {% endif %}
    {% endwith %}

    <div class="card shadow-sm">
        <div class="card-body">
            <form action="{{ url_for('sqlmanager_bp.edit_record', table_name=table_name, record_id=record_id) }}"
                  method="post">
                <div class="row">
                    {% for field in structure %}
                    <div class="col-md-6 mb-3">
                        <label for="{{ field.name }}" class="form-label">
                            {{ field.name }}
                            {% if field.is_primary %}
                            <span class="badge bg-primary ms-1">主键</span>
                            {% endif %}
                            {% if 'datetime' in field.type|string|lower %}
                            <span class="badge bg-info ms-1">日期时间</span>
                            {% endif %}
                            {% if not field.nullable and not field.is_primary %}
                            <span class="text-danger">*</span>
                            {% endif %}
                            <small class="d-block text-muted">{{ field.type }}
                                {% if 'datetime' in field.type|string|lower %}
                                （格式：年-月-日 时:分:秒）
                                {% endif %}
                            </small>
                        </label>
                        {% if 'datetime' in field.type|string|lower %}
                        <input type="datetime-local"
                               class="form-control {% if field.is_primary %}bg-light{% endif %}"
                               id="{{ field.name }}"
                               name="{{ field.name }}"
                               {% if not field.nullable and not field.is_primary %}required{% endif %}
                               {% if field.is_primary %}readonly{% endif %}
			        value="{{ record[field.name].replace(' ', 'T') if record[field.name] else '' }}"
                                step="1">
			       {% else %}
                        <input type="text"
                               class="form-control {% if field.is_primary %}bg-light{% endif %}"
                               id="{{ field.name }}"
                               name="{{ field.name }}"
                               {% if not field.nullable and not field.is_primary %}required{% endif %}
                               {% if field.is_primary %}readonly{% endif %}
                               value="{{ record[field.name] if (record and field.name in record) else '' }}">

                        {% if not field.nullable and not field.is_primary %}
                        <div class="form-text text-danger">此字段为必填项</div>
                        {% endif %}
                        {% endif %}
                    </div>
                    {% endfor %}
                </div>

                <div class="d-grid gap-2 d-md-flex justify-content-md-end mt-4">
                    <button type="submit" class="btn btn-success">
                        <i class="bi bi-save me-1"></i> 保存更改
                    </button>
                    <a href="{{ url_for('sqlmanager_bp.table_view', table_name=table_name) }}"
                       class="btn btn-outline-secondary">
                        <i class="bi bi-x-circle me-1"></i> 取消
                    </a>
                </div>
            </form>
        </div>
    </div>
</div>

<script src="/Style/js/bootstrap.bundle.min.js"></script>
</body>
</html>
